package com.itheima.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.itheima.common.TokenHolder;
import com.itheima.entity.dto.addressbook.AddressBookUpDto;
import com.itheima.entity.dto.addressbook.AddressBookaddDto;
import com.itheima.entity.pojo.AddressBook;
import com.itheima.entity.vo.addressbook.*;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.service.AddressBookService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author huangzelnag
 * @create 2022-05-20
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {
  @Autowired
    AddressBookMapper addressBookMapper;



    /**
     * 添加收货地址
     *
     * @param addressBookaddDto
     * @return
     */
    @Override
    public AddressBookaddVo addressBookService(AddressBookaddDto addressBookaddDto) {

        AddressBook addressBook = new AddressBook();

        BeanUtils.copyProperties(addressBookaddDto,addressBook);

        String userId = TokenHolder.getCurrentId();
        String id = new Snowflake().nextIdStr();
        addressBook.setId(id);
        addressBook.setUserId(userId);
        addressBook.setCreateUser(userId);
        addressBook.setUpdateUser(id);
        addressBook.setUpdateTime(new Date());
        addressBook.setCreateTime(new Date());

         Boolean flag =  addressBookMapper.insert(addressBook);
        AddressBookaddVo addressBookaddVo = new AddressBookaddVo();

        BeanUtils.copyProperties(addressBook,addressBookaddVo);


        return addressBookaddVo;
    }

    /**
     * 更新收货地址
     *
     * @param addressBookUpDto
     * @return
     */
    @Override
    public Boolean upAddressBook(AddressBookUpDto addressBookUpDto) {
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookUpDto,addressBook);
        addressBook.setUpdateTime(new Date());

       Boolean flag= addressBookMapper.up(addressBook);

        return flag;
    }

    /**
     * 批量删除地址
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean delAddressBook(String[] ids) {

      Boolean flag =  addressBookMapper.deleteByIds(ids);

        return flag;
    }

    /**
     * 获取默认地址
     *
     * @return
     */
    @Override
    public AddressBookDefaultVo getAddressBookDefault() {
        String userId = TokenHolder.getCurrentId();

        AddressBookDefaultVo addressBookDefaultVo= addressBookMapper.selectDefault(userId);


        return addressBookDefaultVo;
    }

    /**
     * 设置默认收货地址
     * @param addressBook
     * @return
     */
    @Override
    public AddressBookDefaultSave saveAddressBookDefault(AddressBook addressBook) {
        addressBook.setIsDefault(1);

        addressBookMapper.up(addressBook);
        AddressBookDefaultSave addressBookDefaultSave = new AddressBookDefaultSave();

        addressBookDefaultSave.setIsDefault(1);

        return addressBookDefaultSave;
    }

    /**
     * 获取收货地址列表
     *
     * @return
     */
    @Override
    public List<AddressBookListVo> getAddressBookList() {
        String userId = TokenHolder.getCurrentId();


        List<AddressBookListVo>  addressBookListVos=  addressBookMapper.selectList(userId);




        return addressBookListVos;
    }

    /**
     * 根据id获取收货地址
     *
     * @param id
     * @return
     */
    @Override
    public AddressBookByIdVo getAddressBookById(String id) {

        AddressBookByIdVo addressBookByIdVo=  addressBookMapper.selectById(id);




        return addressBookByIdVo;
    }


}
